<?php 

namespace town\base {
    $app = new App(true);
    $app->run();
}

namespace {

    use town\ting\Ting;    
    use town\base\Store;
    use town\base\Response;

    function view($name, $datas = [])
    {
        $config = [
            'spacePath'  => APP_PATH.DS.'business'.DS.'view',
            'parsePath'  => APP_PATH.DS.'runtime'.DS.'view'.DS.'parse',
            'staticPath' => APP_PATH.DS.'runtime'.DS.'view'.DS.'static',
            'staticTime' => 20,
        ];
        $view = new Ting($config, APP_DEBUG);
        $cacheKey = is_array($datas) ? null : $datas;
        $text = $view->setTemplate($name, $cacheKey);
        if (is_string($text) && !empty($text)) {
            return response()->content($text);
        }

        // 注入系统常量 csrf_token
        if (is_array($datas)) {
            $datas = array_merge($datas, Store::val());
            foreach ($datas as $key => $value) {
                $view->setParameter($key, $value);
            }
        }

        $text = $view->fetchParseText();
        return response()->content($text);
    }

    function abort($code)
    {
        return response()->abort($code);
    }

    function response()
    {
        return Response::singleton();
    }

    function redirect($url = '', $abs = false)
    {
        if (empty($url)) {
            $url = '/';
        }
        return response()->redirect($url, $abs);
    }
}